package com.qianbei.common.net.control;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.qianbei.common.net.control.ServerControl.ServerListener;
import com.qianbei.common.net.view.ProgressDialog;

public class BaseNetActivity extends FragmentActivity implements
		ServerListener, Constant {

	protected ServerControl mServerControl;

	protected Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
	}


	/** 全局初始化 网络请求 去掉重复代码 */
	protected ServerControl creatServerControl(int methd,String ul, Object... key_value) {

		ServerControl sc = null;
		if (sc == null) {
			sc = new ServerControl(methd,ul, key_value);
		}
		sc.serverListener = this;
		return sc;
	}

	protected void initServer(int methd,String ul, Object... key_value) {
		if (mServerControl == null) {
			mServerControl =  creatServerControl(methd,ul, key_value);
		}
		startControl();
	}

	protected void initAlertServer(int methd,String ul, Object... key_value) {
		if (mServerControl == null) {
			mServerControl =creatServerControl(methd,ul, key_value);
		}
		startControlAlert();
	}

	/**
	 * 网络回调接口 如果一个 activity 中只有一个请求 ，回掉就用次处 ，否则自定义回掉
	 */
	@Override
	public void serverFinish(ServerResult sr) {

	}

	protected void startControlAlert() {
		if (mServerControl != null) {
			new ProgressDialog(mContext).startControl(mServerControl);
		}
	}

	protected void startControl() {
		if (mServerControl != null) {
			mServerControl.startVolley();
		}
	}

}
